package com.jereh.topnews.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jereh.topnews.R;
import com.jereh.topnews.adapter.NewsListAdapter;
import com.jereh.topnews.api.NewsApi;
import com.jereh.topnews.callback.CallBack;
import com.jereh.topnews.entity.Channel;
import com.jereh.topnews.entity.Content;
import com.jereh.topnews.util.RequestUtil;

import java.util.ArrayList;
import java.util.List;

public class ContextMenuListActivity extends AppCompatActivity implements CallBack{

    private PullToRefreshListView pullListView;
    private ListView listView;
    private List<Content> list=new ArrayList<>();
    private BaseAdapter adapter;
    private RequestUtil util;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_context_menu_list);
        pullListView=(PullToRefreshListView)findViewById(R.id.listView);
        listView=pullListView.getRefreshableView();
        adapter=new NewsListAdapter(this,list);
        listView.setAdapter(adapter);
        util=new RequestUtil(this,this);
        util.setDataByUrl(NewsApi.getNewsListApi(new Channel("5572a108b3cdc86cf39001cf","军事焦点"),1));
        registerForContextMenu(listView);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(Menu.NONE, 0, 0, "是否删除该条新闻");
        menu.setHeaderTitle("删除新闻");
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        //通过菜单项得到菜单信息
        AdapterView.AdapterContextMenuInfo menuInfo= (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        //通过menuInfo的position属性获取Listview选中的那一行下标
        Toast.makeText(ContextMenuListActivity.this, "成功删除该条新闻，标题是："+list.get(menuInfo.position-1).getTitle(), Toast.LENGTH_SHORT).show();
        return super.onContextItemSelected(item);
    }

    @Override
    public void callback(String json) {
        try {
            JSONObject object = JSON.parseObject(json);
            JSONObject body = (JSONObject) object.get("showapi_res_body");
            JSONObject pagebean = (JSONObject)body.get("pagebean");
            JSONArray contentlist=pagebean.getJSONArray("contentlist");
            list.addAll(JSON.parseArray(contentlist.toJSONString(), Content.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void errorCallback() {
        Toast.makeText(ContextMenuListActivity.this, "数据加载失败", Toast.LENGTH_SHORT).show();
    }
}
